﻿using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using TotalApplication.Core;

namespace TotalApplication.AngelSix
{
    /// <summary>
    /// The base class or any content that is being used inside of a <see cref="DialogWindow"/>
    /// </summary>
    public partial class BaseDialogUserControl : UserControl
    {
        private DialogWindow? _messageDialog;

        #region Public Properties

        public int WindowMinimumWidth { get; set; } = 200;

        public int WindowMinimumHeight { get; set; } = 100;

        public int TitleHeight { get; set; } = 30;

        public string Title { get; set; } = "Default Title";

        #endregion

        #region Public Commands

        #endregion

        #region Constructor

        public BaseDialogUserControl()
        {
            // To avoid designer be crazy that we can't see BaseDialogUserControl.xaml content
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                // Create a new window
                _messageDialog = new DialogWindow();
                _messageDialog.ViewModel = new MessageDialogViewModel(_messageDialog);
            }
        }

        #endregion

        #region Public Dialog Show Methods

        /// <summary>
        /// Displays a single message box to the user
        /// </summary>
        /// <param name="viewModel">The message box's view model</param>
        /// <typeparam name="T">The type of the view model for controls</typeparam>
        /// <returns></returns>
        public Task ShowDialogAsync<T>(T viewModel) where T : BaseDialogViewModel,new()
        {
            var tcs = new TaskCompletionSource<bool>();

            Task.Run(() => {
                Application.Current.Dispatcher.Invoke(() =>
                {
                    try
                    {
                        // Match controls expected sizes to the dialog window model
                        _messageDialog.ViewModel.MinimizedHeight = WindowMinimumHeight;
                        _messageDialog.ViewModel.MinimizedWidth = WindowMinimumWidth;
                        _messageDialog.ViewModel.TitleHeight = TitleHeight;
                        _messageDialog.ViewModel.Title = string.IsNullOrEmpty(viewModel.Title) ? Title : viewModel.Title;

                        // Set this control to the dialog window content
                        _messageDialog.ViewModel.Content = this;

                        // Setup the dialog window owner and startup location
                        _messageDialog.Owner = Application.Current.MainWindow;
                        var activeWindow = Application.Current.Windows.OfType<Window>().FirstOrDefault(w => w.IsActive);

                        // 检测当前最活跃的窗口，如果有就不拘泥于最开始的主窗口了
                        if (activeWindow != null)
                        {
                            // activeWindow 就是当前用户正在操作的窗口
                            _messageDialog.Owner = activeWindow;
                        }
                        _messageDialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;

                        // Setup the controls data context binding to the view model
                        DataContext = viewModel;

                        // Do It
                        _messageDialog.ShowDialog();
                    }
                    finally
                    {
                        tcs.TrySetResult(true);
                    }
                }); 
            });


            return tcs.Task;
        }

        #endregion


    }
    
}
